<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link crossorigin='anonymous' href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container">
        <h2 class="page-header">段子列表</h2>
        <ul>
            
        </ul>
    </div>
    <script>
        //  https://api.apiopen.top/getJoke?type=text

        //获取元素
        let btn = document.querySelector('button');
        let ul = document.querySelector('ul');

        //绑定事件
        // btn.addEventListener('click', function(){
            const xhr = new XMLHttpRequest();
            //预设响应体结果为 JSON
            xhr.responseType = 'json';
            xhr.open('GET','https://api.apiopen.top/getJoke?type=text');
            xhr.send();
            xhr.onreadystatechange = function(){
                //判断
                if(xhr.readyState === 4){
                    if(xhr.status >= 200 && xhr.status < 300){
                        // console.log(xhr.response);
                        //遍历段子数据
                        xhr.response.result.forEach(item => {
                            //创建节点
                            let li = document.createElement('li');
                            //修改 li 标签的文本
                            li.innerHTML = item.text;
                            //插入
                            ul.appendChild(li);
                        });
                    }
                }
            }
        // });
    </script>
</body>
</html>